//
//  YCPhotoViewerViewController.m
//  PhotoViewer
//
//  Created by 唐可雄 on 17/2/12.
//  Copyright © 2017年 swustyc. All rights reserved.
//

#import "YCPhotoViewerViewController.h"

#import "YCPhotoViewerModel.h"
#import "YCPhotoViewController.h"

@interface YCPhotoViewerViewController ()

/**
 数据模型
 */
@property (nonatomic, strong) YCPhotoViewerModel *model;
/**
 图片浏览器
 */
@property (nonatomic, strong) UIPageViewController *vcPage;

@end

@implementation YCPhotoViewerViewController

#pragma mark - UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self configVCPage];
    
    [self updateNaviBarTitle];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Properties

- (YCPhotoViewerModel *)model {
    if (_model == nil) {
        _model = [[YCPhotoViewerModel alloc] init];
    }
    
    return _model;
}

#pragma mark - View Config

- (void)updateNaviBarTitle {
    if (self.model.photoes.count > 1) {
        self.title = [NSString stringWithFormat:@"Photos (%zd of %zd)", self.model.curIndex + 1, (long)self.model.photoes.count];
    } else {
        id<YCPhoto> photo = [self.model.photoes objectAtIndex:self.model.curIndex];
        self.title = [photo title];
    }
}

- (void)configVCPage {
    self.model.photoes = self.photoes;
    
    self.vcPage = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                  navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                options:nil];
    [self.vcPage setViewControllers:@[[self.model viewControllerAtIndex:0]]
                          direction:UIPageViewControllerNavigationDirectionForward
                           animated:NO
                         completion:nil];
    self.vcPage.dataSource = self.model;
    self.vcPage.delegate = self;
    
    [self addChildViewController:self.vcPage];
    [self.view addSubview:self.vcPage.view];
    [self.vcPage didMoveToParentViewController:self];
    
    _pageAnimationFinished = YES;
}

#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    _pageAnimationFinished = NO;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    [self updateNaviBarTitle];
    
    _pageAnimationFinished = YES;
}

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
    UIViewController *curViewController = self.vcPage.viewControllers[0];
    [self.vcPage setViewControllers:@[curViewController]
                          direction:UIPageViewControllerNavigationDirectionForward
                           animated:YES
                         completion:nil];
    
    self.vcPage.doubleSided = NO;
    
    return UIPageViewControllerSpineLocationMin;
}

@end
